package com.xintonghua.bussiness.ui.fragment.cube;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseFragment;
import com.xintonghua.bussiness.bean.CheckingInBean;
import com.xintonghua.bussiness.ui.fragment.cube.checkingIn.CheckInNextActivity;
import com.xintonghua.bussiness.util.MyState;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.addapp.pickers.picker.DatePicker;


/**
 * 考勤
 * 作者：wanxianyi
 * 时间：2018/1/11.
 * 包名：com.xintonghua.bussiness.ui.fragment.cube
 */

public class CheckingInFragment extends BaseFragment {

    @BindView(R.id.chuQinAndTotal)
    TextView chuQinAndTotal;

    @BindView(R.id.rl_normal)
    RelativeLayout rlNormal;
    @BindView(R.id.rl_no)
    RelativeLayout rlNo;
    @BindView(R.id.rl_belate)
    RelativeLayout rlBelate;
    @BindView(R.id.rl_zaoTui)
    RelativeLayout rlZaoTui;

    @BindView(R.id.checkingInDate)
    TextView checkingInDate;
    @BindView(R.id.tv_normal_nums)
    TextView tvNormalNums;
    @BindView(R.id.tv_no_nums)
    TextView tvNoNums;
    @BindView(R.id.tv_belate_nums)
    TextView tvBelateNums;
    @BindView(R.id.tv_zaoTui_nums)
    TextView tvZaoTuiNums;

    Unbinder unbinder;

    private CheckingInBean bean;

    @Override
    public int getLayout() {
        return R.layout.fragment_checking_in;
    }

    @Override
    public void initUI(View view) {
        unbinder = ButterKnife.bind(this, view);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        checkingInDate.setText(year + "-"+month+"-"+day);
        getHttp();
    }

    public void getHttp() {
        HttpCent.getInstance(getActivity()).getRecord(getArguments().getInt(BuildConfig.FLAVOR), checkingInDate.getText().toString() + " 23:59:59", this, MyState.requestOne);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_normal, R.id.rl_no, R.id.rl_belate, R.id.rl_zaoTui,R.id.checkingInDate})
    public void onViewClicked(View view) {
        if (bean == null){
            return;
        }
        Intent intent = new Intent(getActivity(), CheckInNextActivity.class);
        switch (view.getId()) {
            case R.id.rl_normal:
                if (bean.getZhengchangIds()==null || bean.getZhengchangIds().equals("")){
                    Toast.makeText(getActivity(), "没有正常打卡记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                intent.putExtra("ids",bean.getZhengchangIds());
                intent.putExtra("type","zhengchang");
                intent.putExtra("time",checkingInDate.getText().toString());
                startActivity(intent);
                break;
            case R.id.rl_no:
                if (bean.getWeidakaIds()==null || bean.getWeidakaIds().equals("")){
                    Toast.makeText(getActivity(), "没有未打卡记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                intent.putExtra("ids",bean.getWeidakaIds());
                intent.putExtra("type","weidaka");
                intent.putExtra("time",checkingInDate.getText().toString());
                startActivity(intent);
                break;
            case R.id.rl_belate:
                if (bean.getChidaoIds()==null || bean.getChidaoIds().equals("")){
                    Toast.makeText(getActivity(), "没有迟到记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                intent.putExtra("ids",bean.getChidaoIds());
                intent.putExtra("type","chidao");
                intent.putExtra("time",checkingInDate.getText().toString());
                startActivity(intent);
                break;
            case R.id.rl_zaoTui:
                if (bean.getZaotuiIds()==null || bean.getZaotuiIds().equals("")){
                    Toast.makeText(getActivity(), "没有早退记录", Toast.LENGTH_SHORT).show();
                    return;
                }
                intent.putExtra("ids",bean.getZaotuiIds());
                intent.putExtra("type","zaotui");
                intent.putExtra("time",checkingInDate.getText().toString());
                startActivity(intent);
                break;
            case R.id.checkingInDate:
                onYearMonthDayPicker();
                break;
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                Log.e("BUNNIES", "dataBack: " + data.toString());
                bean = JSONObject.parseObject((String) data,CheckingInBean.class);
                chuQinAndTotal.setText(bean.getChuqin() + "/" + bean.getTotal());
                tvNormalNums.setText(bean.getZhengchang()+"");
                tvNoNums.setText(bean.getWeidaka()+"");
                tvBelateNums.setText(bean.getChidao()+"");
                tvZaoTuiNums.setText(bean.getZaotui()+"");
                break;
        }
    }

    public void onYearMonthDayPicker() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        final DatePicker picker = new DatePicker(getActivity());
        picker.setCanLoop(true);
        picker.setWheelModeEnable(true);
        picker.setTopPadding(15);
        picker.setRangeStart(1790, 8, 29);
        picker.setRangeEnd(2111, 1, 11);
        picker.setSelectedItem(year, month, day);
        picker.setWeightEnable(true);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                checkingInDate.setText(year+"-"+month+"-"+day);
                getHttp();
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {

            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }
}
